package psnl.bingo.mr.demo4;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class D4Line implements WritableComparable<D4Line> {
    private String city;
    private String obj;
    private String month;
    private Integer data;

    public D4Line() {
    }

    public D4Line(String city, String obj, String month, Integer data) {
        this.city = city;
        this.obj = obj;
        this.month = month;
        this.data = data;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getObj() {
        return obj;
    }

    public void setObj(String obj) {
        this.obj = obj;
    }

    public String getMonth() {
        return month;
    }

    public void setMonth(String month) {
        this.month = month;
    }

    public Integer getData() {
        return data;
    }

    public void setData(Integer data) {
        this.data = data;
    }

    // 数据分组依据：city+obj+year
    public String groupKey() {
        return city+obj+month;
    }

    @Override
    public int compareTo(D4Line o) {
        return groupKey().compareTo(o.groupKey());
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(city);
        out.writeUTF(obj);
        out.writeUTF(month);
        out.writeInt(data);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        city = in.readUTF();
        obj = in.readUTF();
        month = in.readUTF();
        data = in.readInt();
    }

    @Override
    public String toString() {
        return String.join(";", city, obj, month, data+"");
    }
}
